package com.mty.springboot.controller;

import com.mty.springboot.model.PageInfo;
import com.mty.springboot.model.Score;
import com.mty.springboot.model.User;
import com.mty.springboot.service.ScoreService;
import com.mty.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: 控制层
 * @author: mty
 */
@Controller
@RequestMapping(value = "/score")
public class ScoreController {

    // 依赖注入
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private UserService userService;

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findScore")
    public String findScore(Integer pageIndex, Integer pageSize,String uid, Model model,HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        String type = (String)session.getAttribute("type");
        if(type.equals("03")){
            User user = (User)session.getAttribute("ad");
            mp.put("uid",user.getId());
        }else{
            mp.put("uid",uid);
        }
        PageInfo<Score> pageList = scoreService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        List<User> users = userService.getAll();
        model.addAttribute("users",users);
        return "ScoreList";
    }


    /**
     * 添加
     */
    @RequestMapping(value = "/addScore" ,method = RequestMethod.POST)
    @ResponseBody
    public String addScore( @RequestBody Score score) {
        try{
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM:dd HH:mm:ss");
            String time = simpleDateFormat.format(date);
            score.setCreateTime(time);
            scoreService.addScore(score);
            return "200";
        }catch (Exception e){
            e.printStackTrace();
            return "201";
        }
    }


    /**
     * 删除
     */
    @RequestMapping( "/deleteScore")
    @ResponseBody
    public String deleteScore(String id) {
        int d = scoreService.deleteScore(id);
        return "ScoreList";
    }


    /**
     * 修改
     */
    @RequestMapping( "/updateScore")
    @ResponseBody
    public String updateScore(@RequestBody  Score score) {
        try{
            scoreService.updateScore(score);
            return "200";
        }catch (Exception e){
            e.printStackTrace();
            return "201";
        }
    }


    /**
     * 按照ID查询
     */
    @RequestMapping( "/findScoreById")
    @ResponseBody
    public Score findScoreById(String id,Model model,HttpServletRequest request) {
        Score score= scoreService.findScoreById(id);
        return score;
    }


}
